<?php
class Getpwd_email_model extends BZ_Model
{
    protected $table = 'getpwd_email';

    public function get_data($w, $f="*")
    {
        $this->db->select($f);
        if (!empty($w['user_email'])) {
            $this->db->where('email', $w['user_email']);
        }
        if (!empty($w['verify'])) {
            $this->db->where('cdkey', md5($w['verify']));
        }
        $this->db->where('flag', 0);
        $this->db->where('addtime <', date('Y-m-d H:i:s'));
        $this->db->where('failtime >', date('Y-m-d H:i:s'));
        return $this->db->get($this->table);
    }
    
    public function add($data=array())
    {
        if (!$data) {
            return false;
        }
        $insert_data = array(
            'username' => isset($data['username']) ? trim($data['username']) : '',
            'email'    => isset($data['email']) ? trim($data['email']) : '',
            'cdkey'    => isset($data['cdkey']) ? trim($data['cdkey']) : '',
            'addtime'  => date('Y-m-d H:i:s'),
            'failtime' => date('Y-m-d H:i:s', strtotime('+10 minutes')),
            'flag'     => 0
        );
        return $this->db->insert($this->table, $insert_data);
    }
    
    public function edit($data=array(), $where=array())
    {
        if (!$where) {
            return false;
        }
        $update_data = array(
            'addtime'  => date('Y-m-d H:i:s'),
            'failtime' => date('Y-m-d H:i:s', strtotime('+10 minutes')),
            'flag'     => 0
        );
        if (!empty($data['cdkey'])) {
            $update_data['cdkey'] = $data['cdkey'];
        }
        if (!empty($where['email'])) {
            $this->db->where('email', $where['email']);
        }
        return $this->db->update($this->table, $update_data);
    }
}